package com.longjianghuasheng.logistics.business.order.fragment

import android.os.Bundle
import androidx.fragment.app.viewModels
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseMultiStateFragment
import com.longjianghuasheng.logistics.business.mine.viewmodel.MineViewModel
import com.longjianghuasheng.logistics.business.order.adapter.DriverOrderAdapter
import com.longjianghuasheng.logistics.business.order.adapter.DriverOrderAdapter.Companion.CANCEL_APPLICATION_FINISHED
import com.longjianghuasheng.logistics.business.order.adapter.DriverOrderAdapter.Companion.CANCEL_APPLY_FOR
import com.longjianghuasheng.logistics.business.order.adapter.DriverOrderAdapter.Companion.CANCEL_DELETE_THE_ORDER
import com.longjianghuasheng.logistics.business.order.viewmodel.DriverOrderViewModel
import com.longjianghuasheng.logistics.databinding.FragmentDriverOrderBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.deleteDialog
import com.longjianghuasheng.logistics.ext.initLinerDecoration
import com.longjianghuasheng.logistics.ext.observer
import com.longjianghuasheng.logistics.ext.openPhone

class DriverOrderFragment :
    BaseMultiStateFragment<DriverOrderViewModel, FragmentDriverOrderBinding>() {

    var type = 0

    override fun isLoadMore(): Boolean = true
    override fun isRefresh(): Boolean = true
    private val driverOrderAdapter by lazy {

        DriverOrderAdapter()
    }
    private val mineViewModel by viewModels<MineViewModel>()
    override fun layoutId(): Int = R.layout.fragment_driver_order

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        driverOrderAdapter.run {
            mDatabind.recyclerView.initLinerDecoration(adapter = this)

            setOnItemClickListener { adapter, view, position ->

                navigate(R.id.driverOrderDetailFragment, Bundle().apply {
                    data[position].run {
                        putInt("id", id ?: 0)
                        putInt("status", status ?: 0)
                    }
                })

            }

            phoneListener = {
                mineViewModel.hot_line()
            }

            clickListener = { id, it ->

                mViewModel.id = id
                when (it) {
                    CANCEL_APPLICATION_FINISHED -> mViewModel.driver_order_done()
                    CANCEL_APPLY_FOR -> mViewModel.driver_order_cancel()
                    CANCEL_DELETE_THE_ORDER -> deleteDialog {

                        mViewModel.driver_order_del()
                    }
                    else -> {}
                }


            }


        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.result.observer(this) {
            mViewModel.isLoaded = true

            if (mViewModel.page == 1) {

                if (it.data.isNullOrEmpty()) {
                    mStatefulLayout.showEmpty()
                    return@observer
                }
                driverOrderAdapter.setNewInstance(it.data)
                return@observer
            }

            if (it.data.isNullOrEmpty()) {
                mViewModel.page--
                smartLayout.setNoMoreData(true)
                return@observer
            }
            driverOrderAdapter.addData(it.data ?: mutableListOf())


        }
        mineViewModel.resultHot_line.observer(this) {
            openPhone(it)

        }
        mViewModel.deleteNotify.observer(this) {

            driverOrderAdapter.removeAt(driverOrderAdapter.data.indexOfFirst { it.id == mViewModel.id })
        }
        eventViewModel.onOrderChangeEvent.observeInFragment(this) {
            smartLayout.autoRefresh()

        }

        eventViewModel.onOrderDriverSelectEvent.observeInFragment(this) {

            if (type == it && mViewModel.isLoaded) {
                mViewModel.page = 1
                lazyLoadData()
            }
        }
    }

    override fun initData() {
        super.initData()
        mViewModel.type = type

    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.driver_order_list()
    }

    companion object {


        fun newInstance(type: Int): DriverOrderFragment {
            val args = Bundle()

            val fragment = DriverOrderFragment()
            fragment.arguments = args
            fragment.type = type
            return fragment
        }
    }
}